package com.gp.controller.count;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gp.common.result.JsonResult;
import com.gp.common.utils.StringUtil;
import com.gp.controller.BaseController;
import com.gp.model.dto.Count;
import com.gp.model.dto.CountByPriceSection;
import com.gp.model.dto.CountByProvince;
import com.gp.model.entity.BaseOrder;
import com.gp.model.entity.BaseUser;
import com.gp.service.IBaseAccessLogsService;
import com.gp.service.IBaseOrderService;
import com.gp.service.IBaseUserService;
import com.gp.service.impl.DwServiceImpl;


/**
*@date: 2018年1月11日
*@author: 弓成龙
*@email: gcx909109@sina.cn
*@description: 数据分析
**/
@RestController
@RequestMapping(value="/count")
public class DwController extends BaseController{
  
  @Autowired
  private IBaseUserService userService;
  @Autowired
  private DwServiceImpl dwService;
  @Autowired
  private IBaseOrderService orderService;
  @Autowired
  IBaseAccessLogsService accesslogService;
  
  /**
   * 商品 》 地区： 计算各个地区(省)各有多少个商品
   */
  @RequestMapping("/countGoodsByProvince")
  public JsonResult countGoodsByProvince(){
    Page<CountByProvince> gc= dwService.countGoodsByProvince();
    return new JsonResult("检索成功",gc);
  }
  
  /**
   * 商品 》 价格： 计算各个价格区段各有多少个商品
   */
  @RequestMapping("/countGoodsByPriceSection")
  public JsonResult countGoodsByPriceSection(){
    Page<CountByPriceSection> gc= dwService.countGoodsByPriceSection();
    return new JsonResult("检索成功",gc);
  }
  
  /**
   * 3.订单 》 下单量： 计算订单日下单量（当周，当月）
   */
  @RequestMapping("/countOrderByDay")
  public JsonResult countOrderByDay(){
    Page<Count> gc= dwService.countOrderByDay();
    return new JsonResult("检索成功",gc);
  }
  
  /**
   *  4.交易额 》 计算日交易额（当周，当月）单位为分
   */
  @RequestMapping("/countTradeAmountByDay")
  public JsonResult countTradeAmountByDay(){
    Page<Count> gc= dwService.countTradeAmountByDay();
    return new JsonResult("检索成功",gc);
  }
  
  /**
   * 5.分销商 》 地区： 计算各个地区的分销商（为市场扩展做数据支撑）
   */
  @RequestMapping("/countUserByProvince")
  public JsonResult countUserByProvince(){
    Page<CountByProvince> gc= dwService.countUserByProvince();
    return new JsonResult("检索成功",gc);
  }
  
  /**
   * 6.分销商 》 收入： 计算各个分销商的收入以及大多数分销商的收入是属于哪一个区段
   */
  @RequestMapping("/countDistributorByIncomeSection")
  public JsonResult countDistributorByIncomeSection(){
    Page<CountByPriceSection> gc= dwService.countDistributorByIncomeSection();
    return new JsonResult("检索成功",gc);
  }
  
  /**
   * 7.分销商 》 收入： 按照层级，计算各个分销商的收入 
  
  @RequestMapping("/countDistributorByIncomeSection")
  public JsonResult countDistributorByIncomeSection(String level){
    Page<CountByPriceSection> gc= dwService.countDistributorByIncome(level);
    return new JsonResult("检索成功",gc);
  } */
  
  /**
   * 8.系统 》 浏览量： 计算前台商城各个页面的浏览量
   
  @RequestMapping("/countDistributorByIncomeSection")
  public JsonResult countDistributorByIncomeSection(String level){
    Page<CountByPriceSection> gc= dwService.countDistributorByIncome(level);
    return new JsonResult("检索成功",gc);
  }*/
  
  /**
   *  新订单
   */
  @RequestMapping("/countNewOrder")
  public JsonResult countNewOrder(){
    Wrapper<BaseOrder> wrapper = new EntityWrapper<>();
    wrapper.eq("ORDERSTATE", "已完成");
    wrapper.between("CREATETIME",StringUtil.obtinDateStrBeforeWeek(),StringUtil.obtinDateStrCurrentTime());
    int selectCount = orderService.selectCount(wrapper);
    return new JsonResult("检索成功",selectCount);
  }
  
  /**
   *   分销商的个数
   */
  @RequestMapping("/countUsers/{level}")
  public JsonResult countOneLevelUser(@PathVariable(value="level") int level){
    String s = "";
    if(level==1) s = "一级分销商";
    if(level==2) s = "二级分销商";
    if(level==3) s = "三级分销商";
    Wrapper<BaseUser> wrapper = new EntityWrapper<>();
    wrapper.eq("CURRENTTYPE", s);
    int selectCount = userService.selectCount(wrapper);
    return new JsonResult("检索成功",selectCount);
  }
  
  /**
   *   新用户
   */
  @RequestMapping("/countNewUsers")
  public JsonResult countNewUser(){
    Wrapper<BaseUser> wrapper = new EntityWrapper<>();
    wrapper.between("CREATETIME",StringUtil.obtinDateStrBeforeWeek(),StringUtil.obtinDateStrCurrentTime());
    int selectCount = userService.selectCount(wrapper);
    return new JsonResult("检索成功",selectCount);
  }
  
  /**
   *   网站访问量
   */
  @RequestMapping("/countSiteAccess")
  public JsonResult countSiteAccess(){
    Wrapper<BaseUser> wrapper = new EntityWrapper<>();
    List<Count> c1= accesslogService.countSiteAccessPV();
    List<Count> c2= accesslogService.countSiteAccessPV();
    Map<String, List<Count>> map = new HashMap<String, List<Count>>();
    map.put("pv", c1);
    map.put("uv", c2);
    return new JsonResult("检索成功",map);
  }
  
  /**
   *   各浏览器使用占比
   */
  @RequestMapping("/countBrowser")
  public JsonResult countBrowser(){
    Wrapper<BaseUser> wrapper = new EntityWrapper<>();
    List<Count> c1= accesslogService.countBrowser();
    return new JsonResult("检索成功",c1);
  }
  
  /**
   *  艾乐贝贝销量及增长量
   */
  @RequestMapping("/countSaleAndIncrease")
  public JsonResult countSaleAndIncrease(){
    Wrapper<BaseUser> wrapper = new EntityWrapper<>();
    List<Count> c1= orderService.countSale();
    List<Count> c2= orderService.countIncrease();
    Map<String, List<Count>> map = new HashMap<String, List<Count>>();
    map.put("sale", c1);
    map.put("increase", c2);
    return new JsonResult("检索成功",map);
  }
}
